from bilibili_api import live, Credential,sync
import time
import socket
import struct
import datetime
import threading
import bilibili_api.utils.Danmaku

usrtime=0
room1 = live.LiveDanmaku(22658118)
SESSDATA = " "
BILI_JCT = " "
BUVID3 = ""
DEDEUSERID=" "
credential = Credential(sessdata=SESSDATA, bili_jct=BILI_JCT, buvid3=BUVID3,dedeuserid=DEDEUSERID )
room2 = live.LiveRoom(22658118, credential)

def mythread1():
    global room1
    sync(room1.connect())

def mythread2():
    global usrtime,room2
    while True:
        hour1=datetime.datetime.now().hour
        #print(hour1)
        print(usrtime)
        if usrtime==15:
            try:
                #sync(room2.ban_user(uid=429529007))
                #sleep(2)
                #sync(room2.ban_user(uid=473056459))
                #sync(room2.ban_user(uid=436728997))
                print("ban")
            except:
                print("ban err")
        if usrtime>0 :
            usrtime=usrtime-1
        else:
            usrtime=0
            try:
                list1=sync(room2.get_black_list())
                banN=list1.get('total')
                if banN>0:
                    data=list1.get('data')
                    for i in range(0,banN):
                        id=data[i].get('id')
                        name=data[i].get('tname')
                        print("解封"+name)
                        sync(room2.unban_user(block_id=id))
            except:
                print("get err")
        time.sleep(2)

# 创建两个线程
ta = threading.Thread(target=mythread1)
tb = threading.Thread(target=mythread2)
ta.start()
tb.start()

@room1.on('DANMU_MSG')
async def on_danmaku(event):
    # 收到弹幕
    #print(event)
    global usrtime
    ll=event.get('data').get('info')
    msg=ll[1]
    tmp1=ll[2]
    uname=tmp1[1]
    uid1=tmp1[0]
    print(uname)
    print(msg)
    if uname == "亚瑟鲁班妲己" and msg =="a":
        usrtime=100
    elif uname == "亚瑟鲁班妲己" and msg =="b":
        usrtime=0
    elif msg=="禁言半分钟":
        usrtime=15
        sync(room2.ban_user(uid=uid1))
@room1.on('SEND_GIFT')
async def on_gift(event):
    # 收到礼物
    print(event)
